package com.allwees.bs.c.module.product.repository;

import com.allwees.bs.c.module.base.repository.BaseRepository;
import com.allwees.bs.c.module.product.entity.ProductEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

public interface ProductRepository extends BaseRepository<ProductEntity, Integer> {

    @Query("SELECT p FROM ProductEntity p WHERE p.isDeleted = 0 AND p.uuid in :uuids")
    List<ProductEntity> getByUuids(@Param("uuids") Set<String> uuids);

    @Query("FROM ProductEntity WHERE uuid=:uuid AND isDeleted = 0")
    ProductEntity getByUuid(String uuid);

    @Query(value = "SELECT * FROM prod_product WHERE is_deleted = 0 AND status = 0 AND (def_no=(?1) OR MATCH(name, keywords) AGAINST(?1)) ORDER BY score DESC,id desc",
            countQuery = "SELECT COUNT(*) FROM prod_product WHERE is_deleted = 0 AND status = 0 AND (def_no=(?1)  OR MATCH(name, keywords) AGAINST(?1))",
            nativeQuery = true)
    Page<ProductEntity> search(@Param("keywords") String keywords, Pageable pageable);

    /**
     * 通用的翻页获取所有商品方法
     *
     * @param pageable 翻页参数
     * @return 标准分页数据
     */
    @Query(value = "SELECT p FROM ProductEntity p WHERE p.isDeleted = 0 AND p.status = 0 ORDER BY p.score DESC,p.id desc",
            countQuery = "SELECT COUNT(p) FROM ProductEntity p WHERE p.isDeleted = 0 AND p.status = 0")
    Page<ProductEntity> findPageAll(Pageable pageable);

    @Query(value = "SELECT p FROM ProductEntity p WHERE p.storeUuid = :storeUuid AND p.isDeleted = 0 AND p.status = 0 ORDER BY p.score DESC,p.id desc",
            countQuery = "SELECT COUNT(p) FROM ProductEntity p WHERE p.storeUuid=:storeUuid AND p.isDeleted = 0 AND p.status = 0")
    Page<ProductEntity> findStoreProducts(@Param("storeUuid") String storeUuid, Pageable pageable);

    /**
     * 根据商品分类获取商品 标准翻页方法
     *
     * @return 带翻页列表数据和对应的分类商品总数统计
     */
    @Query(value = "SELECT p FROM ProductEntity p"
            + " WHERE p.isDeleted = 0 AND p.status = 0 AND p.categoryNo IN :categoryNos"
            + " ORDER BY p.score DESC,p.id desc",
            countQuery = "SELECT COUNT(p) FROM ProductEntity p WHERE p.isDeleted = 0 AND p.status = 0 AND p.categoryNo IN :categoryNos")
    Page<ProductEntity> findCategoryProducts(@Param("categoryNos") List<String> categoryNos, Pageable pageable);

    @Modifying
    @Query(value = "update ProductEntity p set p.totalCollections=p.totalCollections-1 where p.uuid in :uuids")
    void decreaseTotalCollectionsByIds(@Param("uuids") List<String> uuids);

    @Modifying
    @Query("update ProductEntity p set p.totalCollections=p.totalCollections+1 where p.uuid=:uuid")
    void increaseTotalCollectionsById(@Param("uuid") String uuid);

    @Query(value = "SELECT * FROM prod_product p WHERE p.is_deleted = 0 AND p.status = 0 AND p.category_no = ?1 ORDER BY p.score DESC limit ?2",
            nativeQuery = true)
    List<ProductEntity> findRelatedProducts(String keyCode, int count);

    //===========================营销区域=============

    @Modifying
    @Query(value = "update prod_product set total_reviews = total_reviews + 1 where uuid = ?1", nativeQuery = true)
    void updateAppendTotalReviews(String uuid);
}
